/*
 * @Author: xzy 1453077527@qq.com
 * @Date: 2025-08-24 00:34:58
 * @LastEditors: xzy 1453077527@qq.com
 * @LastEditTime: 2025-08-26 17:02:49
 * @Description:
 */
package com.xtedu.examproject.dao.impl;

import com.xtedu.examproject.dao.base.ClassRoomDao;
import com.xtedu.examproject.entity.ClassRoom;
import com.xtedu.examproject.util.DBUtil;
import com.xtedu.examproject.util.PageHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ClassRoomDaoImpl implements ClassRoomDao {

    @Override
    public int insert(ClassRoom classRoom) throws Exception {
        String sql = "insert into class_rooms (room_name,subject_id) values(?,?)";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1,classRoom.getName());
            ps.setInt(2,classRoom.getSubjectId());
            return ps.executeUpdate();
        }
    }

    @Override
    public int update(ClassRoom classRoom) throws Exception {
        String sql = "update class_rooms set room_name=?,subject_id=? where id=?";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setString(1,classRoom.getName());
            ps.setInt(2,classRoom.getSubjectId());
            ps.setInt(3,classRoom.getId());
            return ps.executeUpdate();
        }
    }

    @Override
    public int delete(ClassRoom classRoom) throws Exception {
        String sql = "delete from class_rooms where id=?";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1,classRoom.getId());
            return ps.executeUpdate();
        }
    }

    @Override
    public int deleteById(int id) throws Exception {
        return 0;
    }

    @Override
    public ClassRoom findById(int id) throws Exception {
        String sql = "select * from class_rooms where id=?";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1,id);
            ResultSet rs = ps.executeQuery();
            ClassRoom classRoom = new ClassRoom();
            while (rs.next()) {
                classRoom.setId(rs.getInt("id"));
                classRoom.setName(rs.getString("room_name"));
                classRoom.setSubjectId(rs.getInt("subject_id"));
            }
            return classRoom;
        }
    }

    @Override
    public List<ClassRoom> findAll() throws Exception {
        String sql = "select * from class_rooms";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ResultSet rs = ps.executeQuery();
            List<ClassRoom> classRooms = new ArrayList<ClassRoom>();
            while (rs.next()) {
                ClassRoom classRoom = new ClassRoom();
                classRoom.setId(rs.getInt("id"));
                classRoom.setName(rs.getString("room_name"));
                classRoom.setSubjectId(rs.getInt("subject_id"));
                classRooms.add(classRoom);
            }
            return classRooms;
        }
    }

    @Override
    public PageHelper findAllByPage(int pageindex, int pagesize) throws Exception {
        return null;
    }

    @Override
    public PageHelper findPageByRoleIds(int pageindex, int pagesize, int... roleIds) throws Exception {
        return null;
    }

    @Override
    public List<ClassRoom> findBySubjectId(int subjectId) {
        String sql = "select * from class_rooms where subject_id=?";
        try(Connection conn = DBUtil.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql)) {
            ps.setInt(1,subjectId);
            ResultSet rs = ps.executeQuery();
            List<ClassRoom> classRooms = new ArrayList<ClassRoom>();
            while (rs.next()) {
                ClassRoom classRoom = new ClassRoom();
                classRoom.setId(rs.getInt("id"));
                classRoom.setName(rs.getString("room_name"));
                classRoom.setSubjectId(rs.getInt("subject_id"));
                classRooms.add(classRoom);
            }
            return classRooms;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
